﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using TallyBook.BLL.Manger.Expend;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class AccountModification : Form
    {
        private readonly IAccountService<Account> _accountService = new AccountService();
        private readonly List<TextBox> _txtBoxs = new List<TextBox>();
        public Account Account=new Account();

        //public Action<List<string>> getdata;

        public AccountModification()
        {
           
            InitializeComponent();
        }

        public Action RefreshUpdateForm { get; set; }

        /// <summary>
        ///     修改按钮功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnModification_Click(object sender, EventArgs e)
        {
            if (_txtBoxs.Any(item => string.IsNullOrEmpty(item.Text)))
            {
                MessageBox.Show(@"请填写完整的信息");
                return;
            }
            var detection = new Detection();
            var list = _txtBoxs.Where(item => item.Name == "textAccount" || item.Name == "textInitMoney" || item.Name == "textCurrentMoney").ToArray();
            if (list.Any(detection.DetectionMoney))
            {
                MessageBox.Show(@"含非法字符");
                return;
            }

            long accounttypeid = long.Parse(AccountTypeId.Text);
            long accountid = long.Parse(txtId.Text);
            string accountname = txtAccountName.Text;
            decimal initmoney = decimal.Parse(txtInitMoney.Text);
            decimal cormentmoney = decimal.Parse(txtCurrentMoney.Text);
            string comment = txtComment.Text;
            DateTime time = DateTime.Now;
            if (_accountService.Update(accounttypeid, accountid, accountname,
                initmoney, cormentmoney, comment, time) != 0)
            {
                MessageBox.Show(@"修改成功");
                RefreshUpdateForm();
                Hide();
            }
        }

        private void Reset_Click(object sender, EventArgs e)
        {
            _txtBoxs.ForEach(item=>item.Text=null);
        }

        private void AccountModification_Load(object sender, EventArgs e)
        {
            txtId.Text = Account.Id.ToString(CultureInfo.InvariantCulture);
            txtAccountName.Text = Account.Name;
            txtInitMoney.Text = Account.InitMoney.ToString(CultureInfo.InvariantCulture);
            txtCurrentMoney.Text = Account.CurrentMoney.ToString(CultureInfo.InvariantCulture);
            txtComment.Text = Account.Comment;
            AccountTypeId.Text = Account.AccountTypeId.ToString(CultureInfo.InvariantCulture);

            _txtBoxs.Add(txtId);
            _txtBoxs.Add(txtAccountName);
            _txtBoxs.Add(txtInitMoney);
            _txtBoxs.Add(txtCurrentMoney);
            _txtBoxs.Add(txtComment);
            _txtBoxs.Add(AccountTypeId);
        }

       
    }
}